<template>
<div>
    <div>
        <el-button @click="save()">保存</el-button>
    </div>
      <el-form
        label-position="right"
        ref="form"
        :model="form"
        :rules="formRules"
        label-width="100px"
      >
        <el-row v-for="(subModule, index) in subModuleList" :key="subModule"
          >{{ subModule }}:
          <div v-if="index == 0">
            <el-col :span="4" v-for="item in subModuleItemList[index]" :key="item[0]">
              <el-checkbox  :label="item[1]"  v-model="form.data">{{ item[0] }}</el-checkbox>
            </el-col>
          </div>
        </el-row>
      </el-form>
  </div>
  
</template>

<script>
var BaaS = require("minapp-sdk");
let clientID = "9ed0a3da89683e42d793";
BaaS.init(clientID);
export default {
    name: "activity",
    props: {
        module_activity: {
        type: String,
        default: "",
        },
        moduleList:{
            type:Array,
            default:[]
        }
    },
    data(){
        return{
            id:'62e5e3f691261574891d176f',
            subModuleList: [
                "定义性特征",
                "危险人群",
                "危险因素",
                "相关因素",
                "循证知识"
            ],
            subModuleListSet: new Set(),
            subModuleItemList: [
                [
                    [
                        "步态改变",
                        "e.A.1.1"
                    ],
                    [
                        "从事替换动作",
                        "e.A.1.2"
                    ],
                    [
                        "粗大运动技能下降",
                        "e.A.1.3"
                    ],
                    [
                        "短暂偏执",
                        "e.A.1.4"
                    ],
                    [
                        "短暂眼球震颤",
                        "e.A.1.5"
                    ],
                    [
                        "对环境不满意",
                        "e.A.1.6"
                    ],
                    [
                        "对难以维持责任内疚",
                        "e.A.1.7"
                    ],
                    [
                        "对睡眠不满意",
                        "e.A.1.8"
                    ],
                    [
                        "对休息的需要增加",
                        "e.A.1.9"
                    ],
                    [
                        "对周围环境无兴趣",
                        "e.A.1.10"
                    ],
                    [
                        "翻身困难",
                        "e.A.1.11"
                    ],
                    [
                        "反应时间延长",
                        "e.A.1.12"
                    ],
                    [
                        "非恢复性睡眠型态",
                        "e.A.1.13"
                    ],
                    [
                        "非自主觉醒",
                        "e.A.1.14"
                    ],
                    [
                        "功能性能力下降",
                        "e.A.1.15"
                    ],
                    [
                        "好斗",
                        "e.A.1.16"
                    ],
                    [
                        "昏昏欲睡",
                        "e.A.1.17"
                    ],
                    [
                        "昏睡",
                        "e.A.1.18"
                    ],
                    [
                        "活动不协调",
                        "e.A.1.19"
                    ],
                    [
                        "活动范围减少",
                        "e.A.1.20"
                    ],
                    [
                        "活动缓慢",
                        "e.A.1.21"
                    ],
                    [
                        "活动痉挛",
                        "e.A.1.22"
                    ],
                    [
                        "活动时心率异常",
                        "e.A.1.23"
                    ],
                    [
                        "活动时血压异常",
                        "e.A.1.24"
                    ],
                    [
                        "活动性震颤",
                        "e.A.1.25"
                    ],
                    [
                        "精细运动技能下降",
                        "e.A.1.26"
                    ],
                    [
                        "冷漠",
                        "e.A.1.27"
                    ],
                    [
                        "内省",
                        "e.A.1.28"
                    ],
                    [
                        "疲乏",
                        "e.A.1.29"
                    ],
                    [
                        "疲劳",
                        "e.A.1.30"
                    ],
                    [
                        "频繁小睡",
                        "e.A.1.31"
                    ],
                    [
                        "情感减少",
                        "e.A.1.32"
                    ],
                    [
                        "躯体功能失调",
                        "e.A.1.33"
                    ],
                    [
                        "全身乏力",
                        "e.A.1.34"
                    ],
                    [
                        "缺乏精力",
                        "e.A.1.35"
                    ],
                    [
                        "日常功能活动困难",
                        "e.A.1.36"
                    ],
                    [
                        "食欲下降",
                        "e.A.1.37"
                    ],
                    [
                        "手震颤",
                        "e.A.1.38"
                    ],
                    [
                        "睡眠型态改变",
                        "e.A.1.39"
                    ],
                    [
                        "疼痛敏感性增加",
                        "e.A.1.40"
                    ],
                    [
                        "维持日常生活的能力受损",
                        "e.A.1.41"
                    ],
                    [
                        "维持睡眠困难",
                        "e.A.1.42"
                    ],
                    [
                        "维持正常躯体活动的能力受损",
                        "e.A.1.43"
                    ],
                    [
                        "萎靡",
                        "e.A.1.44"
                    ],
                    [
                        "无聊",
                        "e.A.1.45"
                    ],
                    [
                        "无休息感",
                        "e.A.1.46"
                    ],
                    [
                        "心电图（ECG）异常",
                        "e.A.1.47"
                    ],
                    [
                        "性欲改变",
                        "e.A.1.48"
                    ],
                    [
                        "言语减少",
                        "e.A.1.49"
                    ],
                    [
                        "运动性不适",
                        "e.A.1.50"
                    ],
                    [
                        "运动性呼吸困难",
                        "e.A.1.51"
                    ],
                    [
                        "姿势不稳定",
                        "e.A.1.52"
                    ],
                    [
                        "组织破坏",
                        "e.A.1.53"
                    ],
                    [
                        "组织受损",
                        "e.A.1.54"
                    ]
                ],
                [
                    [
                        "暴露于高压电源",
                        "e.A.2.1"
                    ],
                    [
                        "负性生活事件暴露",
                        "e.A.2.2"
                    ],
                    [
                        "既往活动不耐受史",
                        "e.A.2.3"
                    ],
                    [
                        "家族性睡眠瘫痪症",
                        "e.A.2.4"
                    ],
                    [
                        "苛刻的职业",
                        "e.A.2.5"
                    ],
                    [
                        "长期制度化",
                        "e.A.2.6"
                    ],
                    [
                        "长期住院",
                        "e.A.2.7"
                    ]
                ],
                [
                    [
                        "BMI＞第75个百分点（同年龄同性别）",
                        "e.A.4.1"
                    ],
                    [
                        "不愿意开始活动",
                        "e.A.4.2"
                    ],
                    [
                        "持续节律不同步",
                        "e.A.4.3"
                    ],
                    [
                        "持续睡眠卫生不良",
                        "e.A.4.4"
                    ],
                    [
                        "当前的情况不允许从事活动",
                        "e.A.4.5"
                    ],
                    [
                        "动机不足",
                        "e.A.4.6"
                    ],
                    [
                        "非刺激性生活方式",
                        "e.A.4.7"
                    ],
                    [
                        "关节僵硬",
                        "e.A.4.8"
                    ],
                    [
                        "关于可接受活动的文化信念",
                        "e.A.4.9"
                    ],
                    [
                        "活动不耐受",
                        "e.A.4.10"
                    ],
                    [
                        "肌力下降",
                        "e.A.4.11"
                    ],
                    [
                        "肌肉控制下降",
                        "e.A.4.12"
                    ],
                    [
                        "肌肉质量下降",
                        "e.A.4.13"
                    ],
                    [
                        "精力缺乏",
                        "e.A.4.14"
                    ],
                    [
                        "梦游",
                        "e.A.4.15"
                    ],
                    [
                        "耐力下降",
                        "e.A.4.16"
                    ],
                    [
                        "年龄相关的睡眠时相改变",
                        "e.A.4.17"
                    ],
                    [
                        "强体力活动增加",
                        "e.A.4.18"
                    ],
                    [
                        "缺乏保护组织完整性的知识",
                        "e.A.4.19"
                    ],
                    [
                        "缺乏躯体活动意义的知识",
                        "e.A.4.20"
                    ],
                    [
                        "缺乏维持组织完整性的知识",
                        "e.A.4.21"
                    ],
                    [
                        "日均躯体活动量低于同性别和同年龄推荐的活动量",
                        "e.A.4.22"
                    ],
                    [
                        "深夜精神错乱",
                        "e.A.4.23"
                    ],
                    [
                        "失用",
                        "e.A.4.24"
                    ],
                    [
                        "睡眠剥夺",
                        "e.A.4.25"
                    ],
                    [
                        "夜惊",
                        "e.A.4.26"
                    ],
                    [
                        "移动阻碍",
                        "e.A.4.27"
                    ],
                    [
                        "营养状况失衡",
                        "e.A.4.28"
                    ],
                    [
                        "娱乐活动缺乏",
                        "e.A.4.29"
                    ],
                    [
                        "与环境相关的过度刺激",
                        "e.A.4.30"
                    ],
                    [
                        "与环境相关的支持不足",
                        "e.A.4.31"
                    ],
                    [
                        "与环境相关的阻碍",
                        "e.A.4.32"
                    ],
                    [
                        "长期不适",
                        "e.A.4.33"
                    ],
                    [
                        "制动",
                        "e.A.4.34"
                    ]
                ],
                [
                    [
                        "发作性睡病",
                        "e.A.5.1"
                    ],
                    [
                        "感知觉受损",
                        "e.A.5.2"
                    ],
                    [
                        "骨骼肌受损",
                        "e.A.5.3"
                    ],
                    [
                        "骨骼结构完整性改变",
                        "e.A.5.4"
                    ],
                    [
                        "呼吸性疾病",
                        "e.A.5.5"
                    ],
                    [
                        "挛缩",
                        "e.A.5.6"
                    ],
                    [
                        "手术操作",
                        "e.A.5.7"
                    ],
                    [
                        "睡眠呼吸暂停",
                        "e.A.5.8"
                    ],
                    [
                        "睡眠同伴引起的干扰",
                        "e.A.5.9"
                    ],
                    [
                        "睡眠相关性痛性勃起",
                        "e.A.5.10"
                    ],
                    [
                        "睡眠相关遗尿",
                        "e.A.5.11"
                    ],
                    [
                        "循环问题",
                        "e.A.5.12"
                    ],
                    [
                        "医嘱限制活动",
                        "e.A.5.13"
                    ],
                    [
                        "医嘱制动",
                        "e.A.5.14"
                    ],
                    [
                        "原发性中枢神经系统性过度嗜睡",
                        "e.A.5.15"
                    ],
                    [
                        "治疗性隔离",
                        "e.A.5.16"
                    ],
                    [
                        "周期性肢体运动性疾病",
                        "e.A.5.17"
                    ],
                    [
                        "周围神经病变",
                        "e.A.5.18"
                    ]
                ],
                [
                    [
                        "供氧/需氧失衡",
                        "e.A.3.1"
                    ],
                    [
                        "活动缺乏经验",
                        "e.A.3.2"
                    ],
                    [
                        "静坐的生活方式",
                        "e.A.3.3"
                    ],
                    [
                        "移动障碍",
                        "e.A.3.4"
                    ],
                    [
                        "营养不良",
                        "e.A.3.5"
                    ]
                ]
            ],
            subModuleItemListSet:[],
            subModuleItemCodeList:[],
            form:{
               data:[]
            },
            formRules: {},
            submitDate:{},
            
        }
    },
    methods:{
        getSubModuleItem(index,i) {
            let itemSet = new Set();
            let query = new BaaS.Query();
            let overall_eval = new BaaS.TableObject("overall_eval_item");
            query.compare("module", "=", this.module_activity);
            query.compare("submodule", "=", this.subModuleList[i]);
            overall_eval
            .setQuery(query)
            .select(["submodule","item","item_code"])
            .limit(1000)
            .offset(index)
            .find()
            .then((res) => {
                let datas = res.data.objects;
                datas.forEach((data) => {
                    let tempSet = new  Set();
                    tempSet.add(data.item);
                    tempSet.add(data.item_code);
                    itemSet.add(Array.from(tempSet));
                });
                if (res.data.meta.next !== null) {
                    this.getSubModuleItem(index + 1000,i);
                } else {
                    this.subModuleItemList.push(Array.from(itemSet))  
                }
            });
        },
        getSubModule(index) {
            let query = new BaaS.Query();
            let overall_eval = new BaaS.TableObject("overall_eval_item");
            query.compare("module", "=", this.module_activity);
            overall_eval
                .setQuery(query)
                .select(["module", "submodule"])
                .limit(1000)
                .offset(index)
                .find()
                .then((res) => {
                let datas = res.data.objects;
                datas.forEach((data) => {
                    this.subModuleListSet.add(data.submodule);
                });
                if (res.data.meta.next !== null) {
                    this.getSubModule(index + 1000);
                } else {
                    this.subModuleList = Array.from(this.subModuleListSet);
                    console.log(this.subModuleList);
                    for (let i = 0; i < this.subModuleList.length; i++) {
                        this.getSubModuleItem(0,i);
                    }  
                    console.log(this.subModuleItemList); 
                    console.log(this.subModuleItemCodeList);
                }
                });
        },
        getAllItemCode(index,module){
            let tempSet = new Set();
            let query = new BaaS.Query();
            let overall_eval = new BaaS.TableObject("overall_eval_item");
            query.compare("module", "=", module);
            if(index==0){
                    tempSet.add(module)
            }
            overall_eval
                .setQuery(query)
                .select(["module","item_code"])
                .limit(1000)
                .offset(index)
                .find()
                .then((res) => {
                let datas = res.data.objects;
                datas.forEach((data) => {
                  
                   tempSet.add(data.item_code)
                });
                if (res.data.meta.next !== null) {
                    this.getAllItemCode(index + 1000,module);
                } else {
                    this.subModuleItemCodeList.push(Array.from(tempSet))
                }
                });
        },
        init(){
            this.getSubModule(0);
        },
        GetEvaOverall(){
            for(let i=0;i<this.subModuleItemCodeList.length; i++){
                if(this.module_activity == this.subModuleItemCodeList[i][0]){
                    for(let j=1;j<this.subModuleItemCodeList[i].length;j++){
                        if(this.form.data.includes(this.subModuleItemCodeList[i][j])){
                            this.submitDate[this.subModuleItemCodeList[i][j]] = 1;
                        }else{
                            this.submitDate[this.subModuleItemCodeList[i][j]] = 0;
                        }
                    }
                }else{
                    for(let j=1;j<this.subModuleItemCodeList[i].length;j++){  
                        this.submitDate[this.subModuleItemCodeList[i][j]] = 0;
                    }
                }
               
            }
            let submitLast = JSON.stringify(this.submitDate)
            // .replaceAll("\"", "\\\"");
            console.log(submitLast.type);

            return submitLast;
            
        },
        save(){
            let that = this;
            this.$refs.form.validate(valid => {
            if (valid) {
                this.$confirm("确认提交吗？", "提示", {})
                .then(() => {
                    console.log(this.form.data);
                    let eva_overall = that.GetEvaOverall();
                    // let patientNursing = new BaaS.TableObject("patient_nursing");
                    // let record = patientNursing.getWithoutData(that.id);
                    // record.set({'eva_overall':eva_overall})
                    // record.update().then(res => {
                    //     console.log(res);
                    //     this.$message({
                    //     message: "提交成功",
                    //     type: "success"
                    // });
                    // }, err => {
                    //     console.log(err);
                    // })
                })
                .catch(e => {
                    console.log(e);
                });
            }
        });
        }
    },
    created(){
        // this.init();
        for(let i=0;i<this.moduleList.length;i++){
            this.getAllItemCode(0,this.moduleList[i]);
        }
        console.log(this.subModuleItemCodeList);
    }
}
</script>

<style>

</style>